// 清空页面上的错误信息
function clearError( ){
    var ctls=Array.prototype.slice.call(document.getElementsByClassName('errorCtl'));
    ctls.forEach(function(ctl){
       ctl.classList.remove('errorCtl'); 
    });
    document.getElementById('errorInfo').innerText=null;
}

// 显示错误信息
function showError(errorInfo,errorCtls){
    clearError();
    
    if(errorInfo){
        document.getElementById('errorInfo').innerText=errorInfo;
    }
    
    if(errorCtls){
        errorCtls.forEach(function(ctl){
            ctl.classList.add('errorCtl');
        });
    }
}

// 清空控件内的值
function clearValue(ctlId){
    var ctl=document.getElementById(ctlId);
    
    if(!ctl){
        return;
    }
    
    if(ctl.value){
        ctl.value=null;
    }else{
        ctl.innerText=null;  
    }
}

// 给底部按钮添加事件
var btnLeft=document.getElementById('btnBottomLeft');
var btnRight=document.getElementById('btnBottomRight');

if(btnLeft){
    btnLeft.addEventListener('click',function(){
        if(btnLeft.getAttribute('href')!=='#'){
            return;
        }
        
        document.getElementById('cmdType').value='L';
        submitForm();
    });
}

if(btnRight){
    btnRight.addEventListener('click',function(){
        if(btnRight.getAttribute('href')!=='#'){
            return;
        }

        document.getElementById('cmdType').value='R';
        submitForm();
    }); 
}

// 提交页面
function submitForm(){
    if(!check()){
        return;
    }
    document.forms[0].submit();
}

//项目Check
function check(){
    var ctls=Array.prototype.slice.call(document.getElementsByTagName('input'));
    
    if((!mustCheck(ctls))||(!formatCheck(ctls))){
        return false;
    }else{
        return true;
    }
}

//必要项目录入Check
function mustCheck(ctls){
    var errorInfo="";
    var errorCtls=[];
    ctls.forEach(function(ctl){
        var isMust=ctl.getAttribute('isMust');
        if(isMust==='true' && !ctl.value){
            if(ctl.getAttribute('type')==='text' || ctl.getAttribute('type')==='password' ){
                errorCtls.push(ctl);
                errorInfo='请将必要信息录入完整！';
            }else if(ctl.getAttribute('type')==='hidden'){
                errorInfo='请选择操作行！';
            }
        }
    });
    
    if(errorInfo){
        showError(errorInfo,errorCtls);
        return false;
    }else{
        return true;
    }
}

//项目格式Check
function formatCheck(ctls){
    var errorInfo="";
    var errorCtls=[];
    ctls.forEach(function(ctl){
        var colType=ctl.getAttribute('colType');
        switch(colType){
            case 'Tel':
                if(!checkTel(ctl.value)){
                    errorCtls.push(ctl);
                    errorInfo+="电话号码不正确！\n";
                }
                break;
            case 'Num':
                if(!checkNumber(ctl.value)){
                    errorCtls.push(ctl);
                    errorInfo+="数字项目不正确！\n";
                }
                break;
            default:
        }
    });
    
    if(errorInfo){
        showError(errorInfo,errorCtls);
        return false;
    }else{
        return true;
    }
}

//电话号码格式Check
function checkTel(value){
    if((!value) || value.length===0){
        return true;
    }
    
    var reg=/^[0-9]{11}$/g;
    return reg.test(value);
}

//数字Check
function checkNumber(value){
    if((!value) || value.length===0){
        return true;
    }
    
    var reg=/^[0-9]$|^[1-9][0-9]*$|^([1-9][0-9]*\.[0-9]+)$|^[0-9]\.[0-9]+$/g;
    return reg.test(value);
}

//一览项目添加行选择事件
var table=document.getElementsByTagName('table')[0];
if(table){
    var key=table.getAttribute('key');
    Array.prototype.slice.apply(table.rows).forEach(function(row){
        row.addEventListener('click',function(){
            event.selectedRowIndex=row.rowIndex;
        });
    });

    table.addEventListener('click',function(){
        if((!event.selectedRowIndex) || event.selectedRowIndex===0){
            return;
        }
        
        Array.prototype.slice.apply(table.rows).forEach(function(row){
            if(row.rowIndex===event.selectedRowIndex){
                if(row.classList.contains('selected')){   
                    row.classList.remove('selected');
                    document.getElementById('selectedItem').value=null;
                }else{ 
                    row.classList.add('selected');
                    document.getElementById('selectedItem').value=document.getElementById(key+(row.rowIndex-1)).innerText;
                }
            }else{
                row.classList.remove('selected');
            }
        });
    }); 
}